package com.eco.interceptor;

import com.alibaba.fastjson.JSON;
import com.eco.annotation.AuthToken;
import com.eco.entity.User;
import com.eco.service.UserService;
import com.github.benmanes.caffeine.cache.Cache;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.tags.BindTag;

@Component
/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/interceptor/AuthorizationInterceptor.class */
public class AuthorizationInterceptor implements HandlerInterceptor {
    private static final Logger log = LoggerFactory.getLogger((Class<?>) AuthorizationInterceptor.class);

    @Autowired
    Cache<String, Object> caffeineCache;

    @Autowired
    private UserService userService;

    @Override // org.springframework.web.servlet.HandlerInterceptor
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        if (method.getAnnotation(AuthToken.class) == null && handlerMethod.getBeanType().getAnnotation(AuthToken.class) == null) {
            return true;
        }
        String token = request.getHeader("Authorization");
        Map<String, Object> result = new HashMap<>(4);
        if (StringUtils.isBlank(token)) {
            result.put("code", Integer.valueOf(HttpStatus.UNAUTHORIZED.value()));
            result.put("msg", "没有权限！token为空，请先登陆");
            result.put(BindTag.STATUS_VARIABLE_NAME, false);
            PrintWriter writer = response.getWriter();
            try {
                writer.append((CharSequence) JSON.toJSONString(result));
                if (writer != null) {
                    writer.close();
                    return false;
                }
                return false;
            } catch (Throwable th) {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                }
                throw th;
            }
        }
        String userId = null;
        try {
            userId = (String) this.caffeineCache.getIfPresent(token);
        } catch (Exception e) {
            log.error("Cache已经断开闲置的连接");
        }
        if (StringUtils.isNotEmpty(userId)) {
            AuthToken annotation = (AuthToken) method.getAnnotation(AuthToken.class);
            String value = annotation.value();
            if (StringUtils.isEmpty(value)) {
                this.caffeineCache.put(token, userId);
                return true;
            }
            User userRole = this.userService.findByUserIdAndUsername(Integer.valueOf(userId), value);
            if (userRole == null) {
                try {
                    PrintWriter writer2 = response.getWriter();
                    try {
                        result.put("code", Integer.valueOf(HttpStatus.FORBIDDEN.value()));
                        result.put("msg", "失败，没有该操作权限!");
                        result.put(BindTag.STATUS_VARIABLE_NAME, false);
                        writer2.append((CharSequence) JSON.toJSONString(result));
                        if (writer2 != null) {
                            writer2.close();
                        }
                        return false;
                    } catch (Throwable th3) {
                        if (writer2 != null) {
                            try {
                                writer2.close();
                            } catch (Throwable th4) {
                                th3.addSuppressed(th4);
                            }
                        }
                        throw th3;
                    }
                } catch (Exception e2) {
                    log.error(e2.toString());
                    return true;
                }
            }
            this.caffeineCache.put(token, userId);
            return true;
        }
        try {
            PrintWriter writer3 = response.getWriter();
            try {
                result.put("code", Integer.valueOf(HttpStatus.UNAUTHORIZED.value()));
                result.put("msg", "过期失效，请重新登陆");
                result.put(BindTag.STATUS_VARIABLE_NAME, false);
                writer3.append((CharSequence) JSON.toJSONString(result));
                if (writer3 != null) {
                    writer3.close();
                }
                return false;
            } catch (Throwable th5) {
                if (writer3 != null) {
                    try {
                        writer3.close();
                    } catch (Throwable th6) {
                        th5.addSuppressed(th6);
                    }
                }
                throw th5;
            }
        } catch (Exception e3) {
            log.error(e3.toString());
            return true;
        }
    }

    @Override // org.springframework.web.servlet.HandlerInterceptor
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override // org.springframework.web.servlet.HandlerInterceptor
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}